/********************************************************************
	created:	2010/11/15
	created:	15:11:2010   15:12
	filename: 	e:\SVN\服务器端\Source\ModLogin\async_func.cpp
	file path:	e:\SVN\服务器端\Source\ModLogin
	file base:	async_func
	file ext:	cpp
	author:

	purpose:
*********************************************************************/
#pragma  once
#include <algorithm>
#include <functional>
#include "async_func.h"

/*
select * from
(
    select S.*, T.HasAttachment, T.IsDelete AS IsDelete, T.MailID As existId
    from
    SysMailTable AS S
    left join
    (select * from SysMailStat where Owner='123') AS T
    on T.MailID = S.MailID or T.MailID IS NULL
    ) AS RESULT
    where IsDelete = 0 or IsDelete is null
*/

//获取邮件列表的结构
struct MailListItem
{
	string mailid;		//邮件的ID  Guid
	string title;		//邮件的标题
	string sender;		//邮件的发送者 //wstring
	string receiver;	//邮件的接受者 //wstring
	int isReaded;		//邮件是否已读
	string send_time;	//邮件的发送时间
	int hasAttach;		//邮件是否有附件
	time_t tm_send_time;
};

//异步函数调用的发送邮件
bool SendMail(sql::Statement* pConn, const char* owner, const char* Title, const char* Sender, const char* ReceName,
			  int& nErrorCode, const char* MailAttach,const char* MailContent);

//异步函数调用的获取邮件列表
bool MailList(sql::Statement* pConn, const char* PlayerName,int& nErrorCode, vector<MailListItem*>& maillist, bool& hasSysmail);

//异步函数调用的阅读邮件
bool ReadMail(sql::Statement* pConn, const char* PlayerName,
              const char* mail_guid, string& content, string& attach, int& nErrorCode);

//异步函数调用的删除邮件
bool DeleteMail(sql::Statement* pConn, const char* PlayerName,const char* MailId, int& nErrorCode);

//异步函数调用的获取邮件附件
bool attachmentogin(sql::Statement* pConn, const char* mail_guid, string& attach, const char* receiver, int& nErrorCode);

//异步函数调用发送群邮件
bool SendMailAll(sql::Statement* pConn,  const char* Title, int& nErrorCode, const char* MailAttach,const char* MailContent);

int getMailCountPersonal(sql::Statement* pConn,const char* ownerName){

	int count = 0;

	MysqlResult res;

	char sql[256];
	sprintf(sql, "SELECT COUNT(*) FROM MailTable WHERE Owner='%s' and Sender !='系统'",ownerName);

	res.dataSet = pConn->executeQuery(sql);

	if (res.dataSet)
	{
		if (res.dataSet->next())
		{
			//count = res.dataSet->getInt(0);
			count = res.dataSet->getInt("COUNT(*)");
		}
	}

	return count;
}

// 发送邮件
void AsyncProc_send_mail(CMsgTyped* pmsg, sql::Statement* pConn)
{
	/*
	 * 可能需要判断当前玩家的邮件数量。即使是30天后会自动删除。？
	 */

	if (!pmsg)
	{

		return;
	}

	try
	{
		PersistID connId;
		int receiver = pmsg->IntVal();
		int msgId = pmsg->IntVal();
		connId.nIndex = pmsg->IntVal();
		connId.nIdent = pmsg->IntVal();

		const char* owner = pmsg->StringVal(); //pmsg->WideStrVal();
		const char* Title = pmsg->StringVal();
		const char* Sender = pmsg->StringVal(); //pmsg->WideStrVal();
		const char* ReceName = pmsg->StringVal(); //pmsg->WideStrVal();
		const char* MailAttach = pmsg->StringVal();
		const char* MailContent = pmsg->StringVal();
		int senRet = pmsg->IntVal();
		int bcopper = pmsg->IntVal();
		int copper = pmsg->IntVal();

		int nErrorCode = 0;

		if( getMailCountPersonal(pConn,ReceName) >= 200 ){ //1 100 200 300 400 500
			nErrorCode = GE_MAX_MAIL_COUNT;
		}else{
			SendMail(pConn,owner, Title, Sender, ReceName, nErrorCode, MailAttach,MailContent);
		}

		if (senRet == 1)
		{
			CMsgTyped ret;
			ret.SetInt(0);
			ret.SetInt(receiver);
			ret.SetInt(msgId);
			ret.SetInt(nErrorCode);
			ret.SetString(MailAttach);
			ret.SetString(Sender);
			ret.SetString(ReceName);
			ret.SetString(Title);
			ret.SetInt(bcopper);
			ret.SetInt(copper);

			g_pGame->SendMessage(connId, &ret);
		}
	}
	catch (CExceptMsg e)
	{

	}
}

// 发送邮件 系统邮件 不限制数量。
void AsyncProc_send_mail_props(CMsgTyped* pmsg, sql::Statement* pConn)
{
	if (!pmsg)
	{

		return;
	}

	try
	{
		PersistID connId;
		int receiver = pmsg->IntVal();
		int msgId = pmsg->IntVal();
		connId.nIndex = pmsg->IntVal();
		connId.nIdent = pmsg->IntVal();

		const char* owner = pmsg->StringVal(); //pmsg->WideStrVal();
		const char* Title = pmsg->StringVal();
		const char* Sender = pmsg->StringVal(); //pmsg->WideStrVal();
		const char* ReceName = pmsg->StringVal(); //pmsg->WideStrVal();
		const char* MailAttach = pmsg->StringVal();
		const char* MailContent = pmsg->StringVal();
		int reason = pmsg->IntVal();

		int nErrorCode = 0;
		SendMail(pConn, owner, Title, Sender, ReceName, nErrorCode, MailAttach,MailContent);

		CMsgTyped ret;
		ret.SetInt(0);
		ret.SetInt(receiver);
		ret.SetInt(msgId);
		ret.SetInt(nErrorCode);
		ret.SetInt(reason);
		ret.SetString(MailAttach);
		ret.SetString(Sender);
		ret.SetString(ReceName);
		ret.SetString(Title);

		g_pGame->SendMessage(connId, &ret);
	}
	catch (CExceptMsg e)
	{

	}
}

bool SendMail(sql::Statement* pConn, const char* owner, const char* Title, const char* Sender, const char* ReceName,
			  int& nErrorCode, const char* MailAttach,const char* MailContent)
{
	/*
	 * 这个判断在上层判断
	 * //如果不是系统邮件，需要判断，发送邮件最大数。
	 */

	if (!pConn)
	{
		nErrorCode = GE_CANNOT_ACCESS_DB;
		return false;
	}

	nErrorCode = GE_OK;

	char *sql = new char[strlen(MailContent) + strlen(MailAttach) + strlen(Title) + 256];

	try
	{
		/*objADOStoredProc.AddInputParameter("@Owner", owner);
		objADOStoredProc.AddInputParameter("@Title", Utf8ToWideStr(Title).c_str());
		objADOStoredProc.AddInputParameter("@Sender", Sender);
		objADOStoredProc.AddInputParameter("@ReceiverName", ReceName);
		objADOStoredProc.AddInputParameter("@MailAttachment", (LPSTR)MailAttach);
		objADOStoredProc.AddInputParameter("@MailContent", Utf8ToWideStr(MailContent).c_str());
		objADOStoredProc.AddReturnValue();*/

		sprintf(sql , "CALL Proc_SendMail('%s','%s','%s','%s','%s','%s')",
		  owner, //WideStrAsUtf8(owner).c_str()
          Title,
          Sender, // WideStrAsUtf8(Sender).c_str()
          ReceName, // WideStrAsUtf8(ReceName).c_str()
          MailAttach, MailContent);

        pConn->executeUpdate(sql);

        return true;

	}
    catch(sql::SQLException & e)
    {
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";

		nErrorCode = GE_CANNOT_ACCESS_DB;
	}

	delete [] sql;

	return false;
}

bool sort_mail_time(MailListItem* m1, MailListItem* m2)
{
    return m1->tm_send_time > m2->tm_send_time;
}

// 获取邮件列表异步处理函数
void AsyncProc_mail_list(CMsgTyped* pmsg, sql::Statement* pConn)
{
	if (!pmsg)
	{
		return;
	}

	try
	{
		PersistID connId;;
		int nErrorCode = 0;
		int IsRead = 1;

		CMsgTyped ret;
		ret.SetInt(0);
		ret.SetInt(pmsg->IntVal()); //receive
		ret.SetInt(pmsg->IntVal()); //id
		connId.nIndex = pmsg->IntVal();
		connId.nIdent = pmsg->IntVal();

		const char* PlayerName = pmsg->StringVal(); //pmsg->WideStrVal();

		vector<MailListItem*> mails;

        bool hasSysmail = false;
		MailList(pConn,PlayerName,nErrorCode, mails, hasSysmail);

		// 按时间排序
        if (hasSysmail) sort(mails.begin(), mails.end(), sort_mail_time);

		std::vector<MailListItem*>::iterator ItMail = mails.begin();
		ret.SetInt( nErrorCode );
		ret.SetInt( (int)mails.size() );
		while( ItMail != mails.end() )
		{
			ret.SetString((*ItMail)->mailid.c_str());
			ret.SetString((*ItMail)->title.c_str());
			ret.SetString( (*ItMail)->sender.c_str() ); //SetWideStr
			ret.SetString( (*ItMail)->receiver.c_str()); //SetWideStr
			ret.SetInt((*ItMail)->isReaded);
			ret.SetString((*ItMail)->send_time.c_str());
			ret.SetInt((*ItMail)->hasAttach); //加入判断邮件附件有无

			delete *ItMail;

			ItMail++;
		}

		g_pGame->SendMessage( connId, &ret);
	}
	catch (CExceptMsg e)
	{

	}
}

// 获取系统邮件
bool SysMailList(sql::Statement* pConn, const char* PlayerName, vector<MailListItem*>& maillist)
{
    MysqlResult res;

	try
	{
		char sql[1024];

		//多加Attachment的判断有无，在SendTime后面
		sprintf(sql, "select * from \
( \
select S.MailID, S.Title, S.SendTime, S.Attachment,  T.IsDelete AS IsDelete, T.MailID As existId from \
 SysMailTable AS S \
left join \
(select * from SysMailStat where Owner=N'%s') AS T \
on T.MailID = S.MailID or T.MailID IS NULL \
) AS RESULT \
where IsDelete = 0 or IsDelete is null", PlayerName ); // WideStrAsUtf8(PlayerName).c_str()

        res.dataSet = pConn->executeQuery(sql);
        if (res.dataSet)
        {
            while (res.dataSet->next())
            {
                //  MailID Title SendTime Attachment MailContent HasAttachment IsDelete
				MailListItem& mi = *(new MailListItem);

				mi.mailid = res.dataSet->getString("MailID");
				mi.mailid = "sys_" + mi.mailid;
				mi.title = res.dataSet->getString("Title");

				mi.sender = "系统"; //Utf8AsWideStr("系统");
				mi.receiver = PlayerName;

				string existId = res.dataSet->getString("existId");
				mi.isReaded = existId.size() ? 1 : 0;
                mi.send_time = res.dataSet->getString("SendTime");
                string Attachment = res.dataSet->getString("Attachment");
                mi.hasAttach = Attachment.size() ? 1 : 0;

                mi.tm_send_time = sscanftime(mi.send_time.c_str());

				maillist.push_back(&mi);
            }
        }
	}
	catch(sql::SQLException & e)
    {
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";
    }

    return true;
}

bool MailList(sql::Statement* pConn, const char* PlayerName,int& nErrorCode, vector<MailListItem*>& maillist, bool& hasSysmail)
{
	if (!pConn)
	{
		nErrorCode = GE_CANNOT_ACCESS_DB;
		return false;
	}

    // 读取系统邮件
	SysMailList(pConn, PlayerName, maillist);

	if (maillist.size() > 0) hasSysmail = true;

	int RestCnt = 200 - maillist.size();

    // 读取普通邮件
	MysqlResult res;

	try
	{
		char sql[256];

		//多加Attachment的判断有无，在SendTime后面
		sprintf(sql, "select MailID,Title,Sender,Receiver,SendTime,Attachment,IsReaded from MailTable where Owner ='%s' order by SendTime desc limit %d",
				PlayerName, RestCnt); // WideStrAsUtf8(PlayerName).c_str()

        res.dataSet = pConn->executeQuery(sql);
		if (res.dataSet)
		{
			while (res.dataSet->next())
			{
				MailListItem &mi = *(new MailListItem);

				mi.mailid = res.dataSet->getString("MailID");
				mi.title = res.dataSet->getString("Title");

				mi.sender = res.dataSet->getString("Sender").c_str(); //Utf8AsWideStr(res.dataSet->getString("Sender").c_str());
				mi.receiver = res.dataSet->getString("Receiver").c_str(); //Utf8AsWideStr(res.dataSet->getString("Receiver").c_str())
				mi.isReaded = res.dataSet->getInt("IsReaded");
                mi.send_time = res.dataSet->getString("SendTime");
                string Attachment = res.dataSet->getString("Attachment");
                mi.hasAttach = Attachment.size() ? 1 : 0;

                mi.tm_send_time = sscanftime(mi.send_time.c_str());

				maillist.push_back(&mi);
			}

			return true;
		}
	}
	catch(sql::SQLException & e)
	{
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";
	}

	return false;
}

// 查看邮件异步处理函数
void AsyncProc_read_mail(CMsgTyped* pmsg, sql::Statement* pConn)
{
	if (!pmsg)
	{

		return;
	}

	try
	{
		PersistID connId;

		CMsgTyped ret;
		ret.SetInt(0);
		ret.SetInt(pmsg->IntVal()); //receive
		ret.SetInt(pmsg->IntVal()); //id
		connId.nIndex = pmsg->IntVal();
		connId.nIdent = pmsg->IntVal();

		const char* mail_guid = pmsg->StringVal();
		const char* PlayerName = pmsg->StringVal(); //pmsg->WideStrVal();

		string mail_content;
		string attach;
		int nErrorCode = GE_OK;
		ReadMail(pConn, PlayerName, mail_guid, mail_content, attach, nErrorCode);

		ret.SetInt(nErrorCode);
		ret.SetString(mail_guid);
		ret.SetString(mail_content.c_str());
		ret.SetString(attach.c_str());

		g_pGame->SendMessage(connId, &ret);

	}
	catch (CExceptMsg e)
	{

	}
}

bool ReadSysMail(sql::Statement* pConn, const char* PlayerName,
                 const char* mail_guid, string& content, string& attach, int& nErrorCode)
{
    const char* mail_guid_fix = mail_guid + 4;

    char sql[1024];

    sprintf(sql, "select * from \
( \
select S.MailID, S.Title, S.SendTime, S.MailContent, S.Attachment, T.IsDelete AS IsDelete, T.HasAttachment as HasAttachment, T.MailID As existId from \
 SysMailTable AS S \
left join \
(select * from SysMailStat where Owner=N'%s') AS T \
on T.MailID = S.MailID or T.MailID IS NULL \
) AS RESULT \
where (IsDelete = 0 or IsDelete is null) AND MailID = '%s'", PlayerName,  mail_guid_fix); //WideStrAsUtf8(PlayerName)

    MysqlResult res;

	try
	{
        res.dataSet = pConn->executeQuery(sql);
        if (res.dataSet && res.dataSet->next())
        {
            content = res.dataSet->getString("MailContent");
            attach = res.dataSet->getString("Attachment");

            string existid = res.dataSet->getString("existId");

            if (existid.size() && res.dataSet->getInt("HasAttachment") == 0)
            {
                attach = "";
            }

            // 插入记录
            if (existid.size() == 0)
            {
                existid = res.dataSet->getString("MailID");
                string sendtm = res.dataSet->getString("SendTime");
                // insert into SysMailStat values('%s', '%s', '%s', 1, 0);
                sprintf(sql, "insert into SysMailStat values('%s', '%s', '%s', 1, 0)",
                        existid.c_str(), PlayerName, sendtm.c_str()); //WideStrAsUtf8(PlayerName).c_str()

                pConn->executeUpdate(sql);
            }

            return true;
        }
	}
    catch(sql::SQLException & e)
	{
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";

        content = "";
        attach = "";
	}

    return false;
}

bool ReadMail(sql::Statement* pConn, const char* PlayerName,
              const char* mail_guid, string& content, string& attach, int& nErrorCode)
{
	if (!pConn )
	{
		nErrorCode = GE_CANNOT_ACCESS_DB;
		return false;
	}

	if (strstr(mail_guid, "sys_") == mail_guid)
	{
	    return ReadSysMail(pConn, PlayerName, mail_guid, content, attach, nErrorCode);
	}

	//nErrorCode = GE_ERROR_ACCOUNT_PASS;

    MysqlResult res;

	try
	{
        char sql[128];
        sprintf(sql, "CALL Proc_ReadMail('%s','','','0')", mail_guid);

        res.dataSet = pConn->executeQuery(sql);

        if (res.dataSet && res.dataSet->next())
        {
            nErrorCode = res.dataSet->getInt("__result");

            if (nErrorCode == GE_OK)
            {
                content = res.dataSet->getString("__content");
                attach = res.dataSet->getString("__attach");

                pConn->getMoreResults();
                return true;
            }
        }

        pConn->getMoreResults();
	}
    catch(sql::SQLException & e)
	{
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";

        nErrorCode = GE_CANNOT_ACCESS_DB;
	}

    return false;
}

// 删除邮件异步处理函数
void AsyncProc_delete_mail(CMsgTyped* pmsg, sql::Statement* pConn)
{
	if (!pmsg)
	{
		return;
	}

	try
	{
		PersistID connId;

		CMsgTyped ret;
		ret.SetInt(0);
		ret.SetInt(pmsg->IntVal()); //receive
		ret.SetInt(pmsg->IntVal()); //id
		connId.nIndex = pmsg->IntVal();
		connId.nIdent = pmsg->IntVal();

		const char* MailId = pmsg->StringVal();
		const char* PlayerName = pmsg->StringVal(); //pmsg->WideStrVal()

		int nErrorCode = 0;
		if (DeleteMail(pConn, PlayerName,  MailId ,nErrorCode))
		{
			ret.SetInt(0);
		}
		else
		{
			ret.SetInt(1);
		}

		ret.SetString(MailId);

		g_pGame->SendMessage(connId, &ret);

	}
	catch (CExceptMsg e)
	{

	}
}

bool DeleteSysMail(sql::Statement* pConn, const char* PlayerName,const char* MailId, int& nErrorCode )
{
    const char* mail_guid_fix = MailId + 4;

    // 执行update insert
    // update SysMailState set IsDelete= 1 where MailID=MailID AND Owner=PlayerName;
    // Insert into SysMailState Values('MailID','PlayerName', )

    char sql[1024];
    sprintf(sql, "update SysMailStat set IsDelete = 1 where MailID='%s' AND Owner=N'%s'", mail_guid_fix,
    		PlayerName ); //WideStrAsUtf8(PlayerName).c_str()

    MysqlResult res;
	try
	{
		int n = pConn->executeUpdate(sql);
		if (n == 0)
		{
		    // 插入记录
		    sprintf(sql, "select  SendTime from SysMailTable where MailID='%s'", mail_guid_fix);

            string SendTime;
            res.dataSet = pConn->executeQuery(sql);
            if (res.dataSet && res.dataSet->next())
            {
                SendTime = res.dataSet->getString("SendTime");
            }

            sprintf(sql, "Insert into SysMailStat Values('%s','%s', '%s', 0, 1)", mail_guid_fix,
            		PlayerName , SendTime.c_str()); //WideStrAsUtf8(PlayerName).c_str()

            pConn->executeUpdate(sql);
		}

		return true;
	}
	catch(sql::SQLException & e)
	{
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";

        return false;
	}

    return false;
}

bool DeleteMail(sql::Statement* pConn, const char* PlayerName, const char* MailId, int& nErrorCode )
{
	if (!pConn)
	{
		nErrorCode = GE_CANNOT_ACCESS_DB;
		return false;
	}

    if (strstr(MailId, "sys_") == MailId)
	{
	    return DeleteSysMail(pConn, PlayerName, MailId, nErrorCode);
	}

	nErrorCode = GE_ERROR_ACCOUNT_PASS;

	try
	{
		char sql[256];
		sprintf(sql, "delete from MailTable where Owner = '%s' AND MailID = '%s'",PlayerName, MailId); // WideStrAsUtf8(PlayerName).c_str()

		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
	{
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";

        return false;
	}

	return true;
}

// 获取邮件附件异步处理函数
void AsyncProc_mail_attachment(CMsgTyped* pmsg, sql::Statement* pConn)
{
	if (!pmsg)
	{

		return;
	}

	try
	{
		PersistID connId;

		CMsgTyped ret;
		ret.SetInt(0);
		ret.SetInt(pmsg->IntVal()); //receive
		ret.SetInt(pmsg->IntVal()); //id
		connId.nIndex = pmsg->IntVal();
		connId.nIdent = pmsg->IntVal();

		const char* mail_guid = pmsg->StringVal();
		const char* receiver = pmsg->StringVal(); //pmsg->WideStrVal();

		string attach;
		int nErrorCode = GE_OK;
		attachmentogin(pConn, mail_guid, attach, receiver, nErrorCode);

		ret.SetInt(nErrorCode);
		ret.SetString(mail_guid);
		ret.SetString(attach.c_str());

		g_pGame->SendMessage(connId, &ret);
	}
	catch (CExceptMsg e)
	{

	}
}

bool SysMailAttach(sql::Statement* pConn, const char* mail_guid, string& attach, const char* receiver, int& nErrorCode)
{
    const char* mail_guid_fix = mail_guid + 4;

    char sql[1024];

    sprintf(sql, "select * from \
( \
select S.MailID, S.Attachment, S.SendTime, T.IsDelete AS IsDelete, T.HasAttachment as HasAttachment, T.MailID As existId from \
 SysMailTable AS S \
left join \
(select * from SysMailStat where Owner=N'%s') AS T \
on T.MailID = S.MailID or T.MailID IS NULL \
) AS RESULT \
where (IsDelete = 0 or IsDelete is null) AND MailID = '%s'", receiver,  mail_guid_fix); //WideStrAsUtf8(receiver).c_str()

    MysqlResult res;

	try
	{
        res.dataSet = pConn->executeQuery(sql);
        if (res.dataSet && res.dataSet->next())
        {
            attach = res.dataSet->getString("Attachment");

            string existid = res.dataSet->getString("existId");

            if (existid.size() && res.dataSet->getInt("HasAttachment") == 0)
            {
                attach = "";
            }

            // 插入记录
            if (existid.size() == 0)
            {
                existid = res.dataSet->getString("MailID");
                string sendtm = res.dataSet->getString("SendTime");

                sprintf(sql, "insert into SysMailStat values('%s', '%s', '%s', 0, 0)",
                        existid.c_str(),receiver , sendtm.c_str()); //WideStrAsUtf8(receiver).c_str()

                pConn->executeUpdate(sql);
            }
            else
            {
                 sprintf(sql, "update SysMailStat set HasAttachment = 0 where Owner=N'%s' AND MailID='%s'"
                        , receiver, existid.c_str()); //WideStrAsUtf8(receiver).c_str()

                 pConn->executeUpdate(sql);
            }

            return true;
        }
	}
    catch(sql::SQLException & e)
	{
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";
        attach = "";
	}

    return false;
}

bool attachmentogin(sql::Statement* pConn, const char* mail_guid, string& attach, const char* receiver, int& nErrorCode)
{
	if (!pConn)
	{
		nErrorCode = GE_CANNOT_ACCESS_DB;
		return false;
	}

    if (strstr(mail_guid, "sys_") == mail_guid)
	{
	    return SysMailAttach(pConn, mail_guid, attach, receiver, nErrorCode);
	}

	nErrorCode = GE_ERROR_ACCOUNT_PASS;

    MysqlResult res;;

	try
	{
	    char sql[256];
	    sprintf(sql, "CALL Proc_MailAttachment('%s','','%s','0')", mail_guid, receiver); //WideStrAsUtf8(receiver).c_str()

	    res.dataSet = pConn->executeQuery(sql);

	    if (res.dataSet && res.dataSet->next())
	    {
	        nErrorCode = res.dataSet->getInt("__result");

	        if (nErrorCode == GE_OK)
	        {
	            attach = res.dataSet->getString("__attach");

	            pConn->getMoreResults();
	            return true;
	        }
	    }

	    pConn->getMoreResults();
	}
    catch(sql::SQLException & e)
	{
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";

		nErrorCode = GE_CANNOT_ACCESS_DB;
	}

	return false;
}

//异步函数调用发送群邮件
void AsyncProc_send_mailAll(CMsgTyped* pmsg, sql::Statement* pConn)
{
	if (!pmsg)
	{

		return;
	}

	try
	{
		PersistID connId;
		int receiver = pmsg->IntVal();
		int msgId = pmsg->IntVal();
		connId.nIndex = pmsg->IntVal();
		connId.nIdent = pmsg->IntVal();

		const char* Title = pmsg->StringVal();
		const char* MailAttach = pmsg->StringVal();
		const char* MailContent = pmsg->StringVal();

		int nErrorCode = 0;
		SendMailAll(pConn, Title, nErrorCode, MailAttach,MailContent);

		CMsgTyped ret;
		ret.SetInt(0);
		ret.SetInt(receiver);
		ret.SetInt(msgId);
		ret.SetInt(nErrorCode);

		g_pGame->SendMessage(connId, &ret);
	}
	catch (CExceptMsg e)
	{

	}
}

//异步函数调用发送群邮件
bool SendMailAll(sql::Statement* pConn, const char* Title, int& nErrorCode, const char* MailAttach,const char* MailContent)
{
    nErrorCode = GE_CANNOT_ACCESS_DB;
    return false;
/*
	if (!pDataBase || !pDataBase->IsOpened())
	{
		nErrorCode = GE_CANNOT_ACCESS_DB;
		return false;
	}

	nErrorCode = GE_ERROR_ACCOUNT_PASS;
	BOOL bIsSuccess=false;

	CADOStoredProc objADOStoredProc(pDataBase, 0);

	try
	{

		objADOStoredProc.AddInputParameter("@Title", Utf8ToWideStr(Title).c_str());
		objADOStoredProc.AddInputParameter("@MailAttachment", (LPSTR)MailAttach);
		objADOStoredProc.AddInputParameter("@MailContent", Utf8ToWideStr(MailContent).c_str());
		objADOStoredProc.AddReturnValue();

		bIsSuccess = objADOStoredProc.Exec("Proc_SendMailAll",0);
		nErrorCode = objADOStoredProc.GetReturnValue();

	}
	catch(_com_error e)
	{
		nErrorCode = GE_CANNOT_ACCESS_DB;
	}

	*/

	return true;

}



